package dao;

import java.sql.SQLException;

import model.NhanvienModel;

public class common {
	public String toURL(String strValue) {   
        if (strValue == null)   
            return "";   
        if (strValue.compareTo("") == 0)   
            return "";   
        return java.net.URLEncoder.encode(strValue);   
    } 
	
	public static String replace(String str, String pattern, String replace) {   
	    if (replace == null) {   
	        replace = "";   
	    }   
	    int s = 0, e = 0;   
	    StringBuffer result = new StringBuffer((int) str.length() * 2);   
	    while ((e = str.indexOf(pattern, s)) >= 0) {   
	        result.append(str.substring(s, e));   
	        result.append(replace);   
	        s = e + pattern.length();   
	    }   
	    result.append(str.substring(s));   
	    return result.toString();   
	} 
	
	public static String toHTML(String value) {   
	    if (value == null)   
	        return "";   
	    value = replace(value, "&", "&");   
	    value = replace(value, "<", "<");   
	    value = replace(value, ">", ">");   
	    value = replace(value, "\"", "&" + "quot;");   
	    return value;   
	}  
	public static java.sql.ResultSet openrs(java.sql.Statement stat, String sql)   
            throws java.sql.SQLException {   
        java.sql.ResultSet rs = stat.executeQuery(sql);   
        return (rs);   
    }  
	public boolean checkquyen(javax.servlet.http.HttpSession session) throws SQLException{
		String o1 = (String)session.getAttribute("userName");   
        NhanvienModel model = new NhanvienModel();
        if(model.checknv(o1))
        	return true;
        else
        	return false;
	}
	
	public boolean userinlogin(javax.servlet.http.HttpSession session){
		String o1 = (String)session.getAttribute("userName");
		if(o1 == "" ||o1 == null){
			return false;
		}
		else
			return true;
	}
	
	public String checkSecurity(int iLevel,   
            javax.servlet.http.HttpSession session,   
            javax.servlet.http.HttpServletResponse response,   
            javax.servlet.http.HttpServletRequest request) {   
        try {   
            Object o1 = session.getAttribute("userName");   
            Object o2 = session.getAttribute("passWord");   
            boolean bRedirect = false;   
            if (o1 == null || o2 == null) {   
                bRedirect = true;   
            }   
            if (!bRedirect) {   
                if ((o1.toString()).equals("")) {   
                    bRedirect = true;   
                } else if ((new Integer(o2.toString())).intValue() < iLevel) {   
                    bRedirect = true;   
                }   
            }   
   
            if (bRedirect) {   
                response.sendRedirect("Login.jsp?querystring="   
                        + toURL(request.getQueryString()) + "&ret_page="   
                        + toURL(request.getRequestURI()));   
                return "sendRedirect";   
            }   
        } catch (Exception e) {   
        }      
        return "";   
    }   
   
}  

